<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class TxddController extends Controller
{
    //
    public function index()
    { 
        $money = 0;
        $num = 0;
        $name = null;
        $names = null;
        $namess = null;
        $id = session()->get("carid");
        foreach($id as $v)
        {
            $data  = DB::table("car")->where("carid",$v)->get();
            // var_dump($data);
            foreach ($data as $key => $value) {
                 $money += $value->prices;
                 $num += $value->goodsnum;
                 $name = $value->goodsname;
                 $names = $value->goodsversion;
                 $namess = $value->goodscolor;
             } 
             // dd($data);
            
             // var_dump(session()->get("goodsname"));

        }
        session(["goodsname"=>$name.$names.$namess]);
        
        $zong = $money+100;
        session(["zong"=>$zong]);
        return view("home.txdd",["money"=>$money,"num"=>$num,"zong"=>$zong,"data"=>$data]);
    }
    public function ajax(Request $request)
    {
    	$upid=$request->input('upid');
    	echo ($data = DB::table("site")->where('upid',$upid)->get());
    	
    	
    }
    public function store(Request $request)
    {
        // dd($request);
    	$messages = [
    		"required" => ":attribute 的字段是必要的",
    	];
    	$this->validate($request, [
            // 'email' => 'required',
            // 'age' => 'required',
            // 'phone' => 'required',
	        'name' => 'required',
	        'phone' => 'required|numeric',
	        'address' => 'required',

    	],$messages);
        $zz = $request->input('address');
        $zh = "";
        foreach ($zz as $v) {
            $zh .= DB::table('site')->where('id',$v)->value('name');
        }
    	$data = $request->only("phone","name");
    	$data['address'] = $zh;
    	$data['uid'] = session("homeuser")->userid;
        $data['goodsid'] = session("goods");
        // dd(session("goods"));
    	DB::table("distribution")->insertGetId($data);
        $a = $request['address'];
        session(['site'=>$a]);
        // dd(session()->get('site'));
        $b = session()->get("goodsname");
        $a = array("detail"=>$b);
        $uid = session()->get("homeuser")->userid;
        // dd($uid);
        DB::table("distribution")->where("uid",$uid)->update($a);
    	return view("home.pay");
    }

    public function destroy($id)
    {
        DB::table("distribution")->where("id",$id)->delete();
        return redirect("home.order");
    }
}
